﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BoardPACDSWS.General.Logs;
using BoardPACUtil.Util;
using BoardPACDSDAO.LogOn;
using BoardPACDSBO.General;
using BoardPACDSBO.Auth;
using BoardPACDSBO.UserMgt;
using BoardPACDSBO.Base;
using BoardPACDAO.Base;
using BoardPACDSDAO.DownloadApp;
using BoardPACDSBO.DownloadApp;

namespace BoardPACDSWS.Base
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Authorize" in code, svc and config file together.
    public class Authorize : IAuthorize
    {
        public Guid GetUserGuid(string userName)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                LogOnDAO templateDAO = new LogOnDAO();
                Guid result = templateDAO.GetUserGuid(userName);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_111_sys," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_111_sys"), new FaultReason("err_111_sys"));
            }
        }
        
        public bool ValidateUser(string userName, string password)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");

                LogOnDAO templateDAO = new LogOnDAO();
                bool result = templateDAO.ValidateUser(userName, password);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public bool IsIPadAllowed(LogOnModel model, RequestHeader request, string format)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(model.UserName=" + model.UserName + "; request.requestIp=" + request.requestIp + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")");
                LogOnDAO templateDAO = new LogOnDAO();
                bool result = templateDAO.IsIPadAllowed(model, request, format);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(model.UserName=" + model.UserName + "; request.requestIp=" + request.requestIp + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")");

                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(model.UserName=" + model.UserName + "; request.requestIp=" + request.requestIp + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")", e);

                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(model.UserName=" + model.UserName + "; request.requestIp=" + request.requestIp + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")", e);

                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public UserMgtModelsMetadata GetUser(string userName)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                LogOnDAO templateDAO = new LogOnDAO();
                UserMgtModelsMetadata result = templateDAO.GetUser(userName);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public List<UserRole> GetUserRoles(List<string> userRoles)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName);

                LogOnDAO templateDAO = new LogOnDAO();
                List<UserRole> result = templateDAO.GetUserRoles(userRoles);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName);
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName, e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName, e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }

        public List<RoleOfUser> GetRoleOfUser(int userId)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userId=" + userId + ")");

                LogOnDAO templateDAO = new LogOnDAO();
                List<RoleOfUser> result = templateDAO.GetRoleOfUser(userId);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userId=" + userId + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userId=" + userId + ")", e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userId=" + userId + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public int GetRegisteredDiviceCount(RequestHeader request)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")");

                LogOnDAO templateDAO = new LogOnDAO();
                int result = templateDAO.GetRegisteredDiviceCount(request);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")", e);

                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + "; request.deviceId=" + request.deviceId + "; request.macAddress=" + request.macAddress + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public MemberModel GetMembershipUser(string userName)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                LogOnDAO templateDAO = new LogOnDAO();
                MemberModel result = templateDAO.GetMembershipUser(userName);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);

                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public MembershipServiceModel GetMembershipService()
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName);
                LogOnDAO templateDAO = new LogOnDAO();
                MembershipServiceModel result = templateDAO.GetMembershipService();
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName);
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName, e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName, e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public bool UnlockUser(string userName)
        {
            try
            {
                //Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                //License.License license = new License.License();
                //if (!license.licenseValidate())
                //{
                //    throw new GenServiceException("err_992");
                //}
                LogOnDAO templateDAO = new LogOnDAO();
                bool result = templateDAO.UnlockUser(userName);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(userName=" + userName + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }
        
        public BaseServiceModel OnAuthorization(int customUserId, int type, int RefId, string actionString, int PasswordExpiryPeriod, string fmt)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(customUserId=" + customUserId + "; type=" + type + "RefId=" + RefId + ")");
                BaseDAO baseDAO = new BaseDAO();
                BaseServiceModel result = baseDAO.Authorize(customUserId, type, RefId, actionString, PasswordExpiryPeriod, fmt, Global.UserName);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(customUserId=" + customUserId + "; type=" + type + "RefId=" + RefId + ")");
                return result;
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(customUserId=" + customUserId + "; type=" + type + "RefId=" + RefId + ")", e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_119," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(customUserId=" + customUserId + "; type=" + type + "RefId=" + RefId + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_119"), new FaultReason("err_119"));
            }
        }

        public DownloadAppModel UpdateDB(string iPaVersion, string dbVersion, int userId)
        {
            try
            {
                DownloadAppDAO downloadDAO = new DownloadAppDAO();
                return downloadDAO.UpdateDB(iPaVersion, dbVersion, userId);
            }
            catch (GenServiceException e)
            {
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                throw new FaultException<GenFault>(new GenFault("err_884"), new FaultReason("err_884"));
            }
        }
    }
}
